<?php
/*
客户管理
*/
//define('Acache_no','1');
class Con_alogs extends Acan{
	public $defaultAct='list';
	public $Conread = '会员';
	public $Actread = array('list'=>'所有日志','save'=>'保存','del'=>'删除','delall'=>'删除所有');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$this->model = Afun_db()->model(Acan_table_alogs);
		//print_r($this->model);
	}
	//--所有客户
	function Act_list(){
		if(empty($this->atitle)){$this->atitle = '所有日志';}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$obj = Afun_acan_new('list');
		$status = array(''=>'所有',200=>200,301=>301,302=>302,400=>400,404=>404,500=>500,501=>501,502=>502);
		$key_name = $this->model->key_name;
		$columnrr = Afun_arr_remove($this->model->comment,array('mark'));//初始栏目
		$column = $obj->column($this->model,$columnrr,$_POST['acolumn']);
		$column_json = $obj->form_json($this->model);
		$column_sop = Afun_arr_remove($this->model->comment,array('id'));
		$all[''] = '所有';
		$where = $this->search($area_val,$post,$column_sop);
		$gcon->select = 'host';
		$gcon->group = 'host';
		$gcon->where = "1".$where;
		$hostrr = $this->model->ofindAll($gcon);
		if($hostrr)
		foreach ($hostrr as $k => $v) {
			$hcon->select = 'count(id) as pv,count(DISTINCT ip) as ip';
			$hcon->where = "host='$v->host' and ".$gcon->where;
			$hrr = $this->model->ofind($hcon);
			$hostrr[$k]->ip = $hrr->ip;
			$hostrr[$k]->pv = $hrr->pv;
		}
		$twidth = array('host'=>10,'uri'=>10);
		$area_val = Ajson_encode($area_val);
		//分页函数
		if(strlen($post['host'])>0){
			$where .= " and a.host = '".$post['host']."'";
		}
		$con->where = "1".$where;
		$con->select = "a.*";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = 'datetime';
		$page->order = 'DESC';
		$page->page = '10';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->ofindAll($con);
		if(is_array($list))
		foreach($list as $k=>$v){
			$listjson[$v->$key_name] = $v;
		}
		$listjson = Ajson_encode($listjson);
		$otherurl['host'] = Afun_otherurl('host');
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_stat(){
		$where = $this->search($area_val,$post,$column_sop);
		$gcon->select = 'host,count(id) as pv,count(DISTINCT ip) as ip';
		$gcon->group = 'host';
		$gcon->where = "1".$where;
		$hostrr = $this->model->ofindAll($gcon);
		foreach ($hostrr as $k => $v) {
			$con->select = 'uri,count(uri) as num';
			$con->group = 'uri';
			$con->order = 'num DESC';
			$con->where = "1".$where." and host='$post[host]'";
			$urirr = $this->model->ofindAll($con);
			$con->select = 'referer,count(referer) as num';
			$con->group = 'referer';
			$con->order = 'num DESC';
			$con->where = "1".$where." and host='$post[host]'";
			$referer = $this->model->ofindAll($con);
			unset($con);
		}
		include $this->Atpl();
	}
	//--获取单用户信息
	function Act_info(){
		if($this->id<1){
			return;
		}
		$info = $this->model->ofindPk($this->id);
		Afun_json($info);
	}
	function search(&$area_val,&$post,$column_sop){
		$datetime = false;
		if($_GET){
			$post = $_GET;
			$area_val = array();
			if(strlen($post['status'])>0){
				$where .= " and a.status=".$post['status']."";
			}
			if($post['status']){
				$where .= " and a.status = '$post[status]'";
			}
			if($post['minute']>0){
				$minute = date("Y-m-d H:i:s",Afun_get_time('time')-$post['minute']*60);
				$where .= " and a.datetime >= '$minute'";
				$datetime = true;
			}
			if($post['day']){
				$where .= " and a.datetime > '$post[day] 00:00:00' and a.datetime <= '$post[day] 23:59:59'";
				$datetime = true;
			}
			if($post['month']){
				$where .= " and a.datetime >= '$post[month]-01 00:00:00' and a.datetime < '".Afun_time_date(Afun_time_ex(strtotime($post['month']),1,'m'))."'";
				$datetime = true;
			}
			if($post['week']){
				$weekend = Afun_time_date(Afun_time_ex($post['week'],7,'d'),'Y-m-d');
				$where .= " and a.datetime >= '$post[week] 00:00:00' and a.datetime < '$weekend 00:00:00'";
				$datetime = true;
			}
			if($post['xian']>1){
				$area_val[1] = $post['sheng'];
				$area_val[2] = $post['shi'];
				$area_val[3] = $post['xian'];
				$where .= " and a.area = '".Ajson_encode($area_val)."'";
			}elseif($post['shi']>1){
				$area = '{"1":"'.$post['sheng'].'","2":"'.$post['shi'].'"';
				$where .= " and a.area LIKE '$area%'";
				$area_val[1] = $post['sheng'];
				$area_val[2] = $post['shi'];
			}elseif($post['sheng']>1){
				$area = '{"1":"'.$post['sheng'].'"';
				$where .= " and a.area LIKE '$area%'";
				$area_val[1] = $post['sheng'];
			}
			if(!empty($post['search'])){
				$sop = $post['sop'];
				if($post['sop']!=''){
					$where .= " and a.".$post['sop']." LIKE '%".$post['search']."%'";
				}else{
					foreach($column_sop as $k=>$v){
						if(empty($whereop)){
							$whereop = "a.".$k." LIKE '%".$post['search']."%'";
						}else{
							$whereop .= " or a.".$k." LIKE '%".$post['search']."%'";
						}
					}
					if($whereop){
						$where .= " and (".$whereop.")";
					}
				}
			}
			if(!$datetime){
				$day = $_GET['day'] = Afun_get_time("Y-m-d");
				$where .= " and a.datetime > '$day 00:00:00' and a.datetime <= '$day 23:59:59'";
				$datetime = true;
			}
		}
		return $where;
	}
	//导入
	function Act_import(){
		$path = $_GET['file'];
		$sql = "LOAD DATA INFILE '$path' INTO TABLE ".Acan_table_alogs." FIELDS TERMINATED BY '|'";
		$rs = Afun_db()->exec($sql);
		Afun_alert("成功导入 $rs 条");
	}
	function Act_mvlog(){
		echo Afun_run_cmd('sudo mv /Anmp/temp/logs/*.log /Anmp/temp/alogs/');
		echo Afun_run_cmd('sudo anmp nginx reopen');
		$path = '/Anmp/temp/alogs/';
		$data = Afunc_file()->ascandiro($path,0,2);
		foreach ($data->file as $k => $v) {
			if($v['name']=='error'){unlink($v['path']);continue;}
			$sql = "LOAD DATA INFILE '".$v['path']."' INTO TABLE ".Acan_table_alogs." FIELDS TERMINATED BY '|'";
			$rs = Afun_db()->exec($sql);
			echo $v['names']."成功导入 $rs 条<br>";
			unlink($v['path']);
		}
	}
	function Act_delself(){
		$ip = $_GET['ip'];
		$host = $_GET['host'];
		$uri = $_GET['uri'];
		$post = $_GET;
		if(!empty($ip) && strlen($ip)>7){
			$rs = $this->model->delete("ip='$ip'");
			echo "成功删除IP:$ip 记录 $rs 条<br>";
		}
		if(!empty($host) && strlen($host)>4){
			$rs = $this->model->delete("host LIKE '$host'");
			echo "成功删除host:$host 记录 $rs 条<br>";
		}
		if(!empty($uri) && strlen($uri)>3){
			$rs = $this->model->delete("uri LIKE '$uri'");
			echo "成功删除uri:$uri 记录 $rs 条<br>";
		}
		include $this->Atpl();
	}
	function Act_autoup(){
		$maxnum = 2000;
		$this->Act_mvlog();
		$con->select = 'id,date';
		$con->where = 'datetime is null';
		$con->limit = $maxnum;
		$data = $this->model->ofindAll($con);
		if($data)
		foreach ($data as $k => $v) {
			$this->model->updatePk(array('datetime'=>date("Y-m-d H:i:s",strtotime($v->date))),$v->id);
		}
		echo "成功更新时间 ".count($data)." 条<br>";
		$con->select = 'id,ip';
		$con->where = 'city is null and ipadd is null';
		$con->limit = $maxnum;
		$data = $this->model->ofindAll($con);
		if($data)
		foreach ($data as $k => $v) {
			$ips = Afun_ip_info($v->ip);
			$this->model->updatePk(array('city'=>$ips['city'],'ipadd'=>$ips['add']),$v->id);
		}
		echo "成功更新IP ".count($data)." 条<br>";
		echo date('Y-m-d H:i:s');
		$limit = ' limit '.$maxnum;
		$this->model->delete("host='d.nice9s.com' $limit");
		$this->model->delete("ip='".$_SERVER['REMOTE_ADDR']."' $limit");
		/*$this->model->delete("uri LIKE '/uc_server/avatar.php?uid=%' $limit");
		$this->model->delete("uri LIKE '/home.php?mod=misc&ac=sendmail&rand=%' $limit");
		$this->model->delete("uri LIKE '/home.php?ac=sendmail%' $limit");
		$this->model->delete("uri LIKE '/misc.php?mod=seccode%' $limit");
		$this->model->delete("uri LIKE '/forum.php?mod=image%' $limit");
		$this->model->delete("uri LIKE '/forum.php?mod=attachment%' $limit");
		$this->model->delete("uri LIKE '/connect.php?mod=login%' $limit");
		$this->model->delete("uri LIKE '/misc.php?mod=secqaa%' $limit");
		$this->model->delete("uri LIKE '/misc.php?mod=patch%' $limit");
		$this->model->delete("uri LIKE '/api/manyou/%' $limit");
		*/
		$this->model->delete("uri LIKE '%.js%' or uri LIKE '%.png%' or uri LIKE '%.gif%' or uri LIKE '%.jpg%' or uri LIKE '%.css%'  $limit");
		Afun_refresh('','',60*1000);
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		$savedata = $_POST['save'];
		if($this->id == 'one'){
			$key = $savedata[$this->model->key_name];
			unset($savedata[$this->model->key_name]);
			if(!empty($key)){
				$msg = $this->model->updatePk($savedata,$key);
			}else{//--添加一个新用户
				$msg = $this->model->save($savedata);
			}
		}elseif(is_array($savedata)){
			foreach($savedata as $i=>$vals){
				$this->model->updatePk($vals,$vals['key']);
			}
		}
		Afun_json($msg);
	}
	function Act_error(){
		$savedata = $_POST['save'];
		if(is_array($savedata)){
			foreach($savedata as $i=>$val){
				$this->model->updatePk("status=-1",$val['id']);
			}
		}
		Afun_refresh('成功','referrer');
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afunc_group()->usergroupdel($this->id);
			echo '删除成功';
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			Afunc_group()->usergroupdel($val);
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
}

?>